package com.wewins.fota.controller;

import com.wewins.fota.bean.bo.TokenUserDetails;
import com.wewins.fota.bean.bo.filter.UserFilterCond;
import com.wewins.fota.bean.dto.PageReq;
import com.wewins.fota.bean.dto.PageRst;
import com.wewins.fota.bean.dto.ResponseRst;
import com.wewins.fota.common.constant.AuthConst;
import com.wewins.fota.common.constant.HttpConst;
import com.wewins.fota.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/auth/user")
public class UserInfoController implements BasicController<TokenUserDetails, UserFilterCond> {
    private final UserInfoService service;

    @Autowired
    public UserInfoController(UserInfoService service) {
        this.service = service;
    }

    @Override
    @PostMapping("/query")
    public ResponseRst<PageRst<TokenUserDetails>> queryAll(PageReq<UserFilterCond> pageReq) {
        PageRst<TokenUserDetails> pageRst = service.queryAll(pageReq);
        return ResponseRst.<PageRst<TokenUserDetails>>builder()
                .data(pageRst)
                .build().withOk();
    }

    @GetMapping("/query/{token}")
    public ResponseRst<TokenUserDetails> queryByToken(HttpServletRequest request, @PathVariable String token) {
        String hToken = request.getHeader(AuthConst.HTTP_HEADER_KEY_AUTHENTICATION);
        if (StringUtils.isEmpty(hToken) || !hToken.equals(token)) {
            return ResponseRst.<TokenUserDetails>builder().build()
                    .withError(HttpConst.STATUS_FORBIDDEN, AuthConst.MSG_FAIL_INVALID_USER_QUERY);
        }
        return ResponseRst.<TokenUserDetails>builder()
                .data(service.findByToken(token))
                .build().withOk();
    }

    @Override
    public ResponseRst<TokenUserDetails> queryById(long id) {
        return null;
    }

    @Override
    public ResponseRst<TokenUserDetails> insert(HttpServletRequest request, TokenUserDetails inputParam) {
        return null;
    }

    @Override
    public ResponseRst<Integer> deleteById(HttpServletRequest request, long id) {
        return null;
    }

    @Override
    public ResponseRst<Integer> update(HttpServletRequest request, TokenUserDetails inputParam) {
        return null;
    }
}
